<?php
error_reporting(0);
require_once('../model/PdoMySQL.class.php');
require_once('../model/config.php');
header("Content-Type: text/html;charset=utf-8");
if ($_SERVER["REQUEST_METHOD"] == "GET"){
    autofp();
}
//创建笼位申请信息
function autofp(){
    $cageid=$_REQUEST['cageid'];
    if (!isset($_REQUEST['cageid']) || empty($_REQUEST['cageid'])) {
        echo "参数错误，信息填写不全";
        return;
    }else{
        //TODO: 获取POST表单数据并保存到数据库
        $PdoMySQL=new PdoMySQL();
        $pdo=new PdoMySQL();
        $sql1="select * from cageapply where cageid  ='{$cageid}'";
        $stmt1=$pdo->prepare($sql1);
        $stmt1->execute();
        $longjia='';
        $bh='';
        $num=0;
        $flag='0';
        while($row1=$stmt1->fetch()){
            $username=$row1['username'];
            $sql2="select * from cage where username ='{$username}' order by cageid asc";
            $stmt2=$pdo->prepare($sql2);
            $stmt2->execute();
            while($row2=$stmt2->fetch()){
                if(substr($row2['cageid'],0,5)==$longjia ) {
                    continue;
                }else{
                    $longjia=substr($row2['cageid'],0,6);
                }
                for($i=1;$i<=72;$i++){
                    $d=($i/9)+65;
                    $bh=$longjia+$d+($i%9);

                    $sql3="select * from cage where cageid ='{$bh}'";
                    $stmt3=$pdo->query($sql3);

                    if($stmt3=='1') {

                    }else{
                        $num=$i;
                        $flag="1";
                        break;
                    }

                }
                if($flag=='1'){
                    break;
                }
            }


            if($flag=='1'){

                ?>
   <input type="text" hidden="true" id="cageid" name="" value="<?php echo $bh; ?>" />

<?
                $sql4="select * from cageapply where cageid  ='{$cageid}'";
                $stmt4=$pdo->prepare($sql4);
                $stmt4->execute();
                while($row4=$stmt4->fetch()) {
                    $sql5="select * from cageapply where cageid  ='{$cageid}'";
                    $stmt5=$pdo->prepare($sql5);
                    $stmt5->execute();
                    while($row5=$stmt5->fetch()) {
                        $cageid1=$bh;
                        $username1=$row5['username'];
                        $number1=$num;
                        $animalsum1=$row5['number'];
                        $formerid1=$cageid;
                        $table='cage';
                        $data=compact('cageid1','username1','number1','animalsum1','formerid1');
                        $res=$PdoMySQL->add($data, $table);
                        $lastInsertId=$PdoMySQL->getLastInsertId();
                        if($res){

                            $sql6="update cageapply set status = 1  where cageid ='{$cageid}'";
                            $stmt6=$pdo->execute($sql6);
                            if($stmt6==1){
                                ?>
<script language="JavaScript">window.alert("自动分配成功,分配在"+document.getElementById("cageid").value),location.href="../view/applycheck.php";</script>


<?php
                            }else{
                                ?>
                                <script language="JavaScript">window.alert("自动分配失效"),location.href="../view/doctorcheck.php?cageid="+<?php echo $cageid; ?>;</script>

                                <?php

                            }


                        }

                    }

                }


            }

        }
    }

}
